package com.ambitor.ipro.labelParser;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.IData;
import com.ambitor.ipro.exceptions.ProException;
import com.ambitor.ipro.service.TopService;
import com.taobao.api.domain.Item;

public class SameCatRandomLabelParser extends IntellLabelParser {

	@SuppressWarnings("unchecked")
	public List<Item> parse() throws ProException {
		String installIid = this.getInstallIid();
		IData userSession = this.getUserSession();
		String sessionId = userSession.getString("SESSION_ID");
		String nick = userSession.getString("NICK");
		String orderString = "delist_time:desc";
		TopService topService = this.getTopService();
		int num = this.getNum();

		Item item = topService.getItem(sessionId, nick, installIid);
		List<Item> sameCatRandomItemList = new ArrayList<Item>();
		if (item != null && StringUtils.isNotBlank(item.getSellerCids())) {
			String sellerCids = item.getSellerCids();
			sameCatRandomItemList = topService
					.getOnSaleItemsOfSpecificSellerCat(sessionId, nick,
							sellerCids, num, orderString);
		}

		if (sameCatRandomItemList.size() <= 1) {
			if (item != null) {
				Long cid = item.getCid();
				sameCatRandomItemList = topService.getOnSaleItemsOfSpecificCat(
						sessionId, nick, cid, num, orderString);
			}

		}
		return sameCatRandomItemList;
		/*
		 * int minus = num - sameCatRandomItemList.size(); if (minus <= 0) {
		 * return sameCatRandomItemList; }
		 * 
		 * IntellLabelParser parser = new LatestListLabelParser(topService,
		 * userSession, num); List<Item> latestListItemList = parser.parse();
		 * int i = 0; List<Item> tempItemList = new ArrayList<Item>(); for
		 * (Item latestListItem : latestListItemList) { if (i == minus) break;
		 * boolean isExists = false; for (Item sameCatItem :
		 * sameCatRandomItemList) { if
		 * (latestListItem.getNumIid().equals(sameCatItem.getNumIid())) {
		 * isExists = true; break; } } if (isExists == false) {
		 * tempItemList.add(latestListItem); i++; } }
		 * sameCatRandomItemList.addAll(tempItemList); return
		 * sameCatRandomItemList;
		 */
	}
}
